/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cannes.dao.implement;

import cannes.bean.planning.Planning;
import cannes.connexion.ConnexionBDD;
import cannes.dao.DAO;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Pierre
 */
public class PlanningDAO extends DAO<Planning>{
    
    public PlanningDAO(Connection conn) {
        super(conn);
    }

    @Override
    public boolean create(Planning obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean update(Planning obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean delete(Planning obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int next() {
        throw new UnsupportedOperationException("Not supported yet.");
    }


    @Override
    public Planning find(int id) {
        Planning planning = new Planning();

        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM planning WHERE planningid = " + id);
            if (result.first()) {
                
                planning = new Planning(result.getInt("planningid"),
                        result.getString("typeselection"));
                ProjectionDAO projDao = new ProjectionDAO(this.connect);


                result = this.connect.createStatement().executeQuery(
                    "SELECT projectionid FROM projection WHERE planningid = " + id);

                while (result.next()) {
                    planning.addProjection(projDao.find(result.getInt("projectionid")));
                }
                
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return planning;

    }

    @Override
    public ArrayList<Planning> findAll() {
        ArrayList<Planning> listePlanning = new ArrayList<Planning>();
        Planning planning;
        
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM planning");
            ResultSet resultProj;
            ProjectionDAO projDao = new ProjectionDAO(this.connect);
            
            while(result.next()) {
                
                planning = new Planning(result.getInt("planningid"),
                        result.getString("typeselection"));


                resultProj = this.connect.createStatement().executeQuery(
                    "SELECT projectionid FROM projection WHERE planningid = " + planning.getId());

                while (resultProj.next()) {
                    planning.addProjection(projDao.find(resultProj.getInt("projectionid")));
                }
                
                listePlanning.add(planning);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return listePlanning;
        
    }
    
}
